//
//  AppProPhoneNumTFView.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/3/17.
//

import UIKit

class AppProPhoneNumTFView: AppProTextFieldBaseView {
    
    override var maxCharacterCount: Int {
        11
    }

    override var kAlphaNum: String {
        "0123456789"
    }
    
    var isPhoneOk: Bool = false
    
    override func addSubViews() {
        super.addSubViews()
        
        icon.image = R.image.login_account_icon()
        
        lineView.isHidden = true
        lineView.backgroundColor = btnBgColor
//        lineView.alpha = 0.08
        
        titleLb.isHidden = true
        titleLb.text = "手机号"
//        button.setImage(R.image.close_gray(), for: .normal)
        
        textField.placeholder = "请输入手机号码"
        textField.setPlaceHolderTextColor(placeHolderTextColor)

        textField.clearButtonMode = .whileEditing
        textField.keyboardType = .asciiCapableNumberPad
        textField.delegate = self
        
//        if let clearBtn = textField.value(forKey: "_clearButton") as? UIButton {
////            clearBtn.setImage(R.image.close_gray(), for: .normal)
//        }
    }
    
    override func relayoutSubViews() {
        relayoutWithTitle()
    }
    
    func relayoutWithTitle() {
        icon.isHidden = false
        titleLb.isHidden = true
        lineView.isHidden = false
        
//        titleLb.snp.remakeConstraints { make in
//            make.left.equalToSuperview().inset(16)
//            make.width.lessThanOrEqualTo(70)
//            make.centerY.equalToSuperview()
//            make.height.equalTo(24)
//        }
        icon.snp.remakeConstraints { make in
            make.size.equalTo(24)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().inset(20)
        }
        
//        lineView.snp.remakeConstraints { make in
//            make.left.equalTo(icon.snp.right).offset(18)
//            make.centerY.equalTo(icon)
//            make.width.equalTo(1)
//            make.height.equalTo(22)
//        }
        
        textField.snp.remakeConstraints { make in
            make.left.equalTo(icon.snp.right).offset(18)
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
        }
    }
    
    func relayoutWithoutTitle() {
        icon.isHidden = true
        titleLb.isHidden = true
        lineView.isHidden = true
    
        removeConstraints(icon.constraints)
        removeConstraints(titleLb.constraints)
        removeConstraints(lineView.constraints)
        textField.snp.remakeConstraints { make in
            make.left.equalToSuperview().inset(20)
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
        }
    }
    
    // MARK: - UITextField
    override func textFieldChanged(sender: UITextField) {
        if textContent.count == 11 {
            isPhoneOk = true
        } else {
            isPhoneOk = false
        }
        
        tfChangedCallback?(textContent)
    }

}

//extension XWHPhoneNumTFView {
//    
//    override func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//        if string.isEmpty {
//            return true
//        }
//        
//        let cs = CharacterSet(charactersIn: kAlphaNum).inverted
//        let filtered = string.components(separatedBy: cs).joined(separator: "")
//        if string == filtered {
//            let textCount = textField.text?.count ?? 0
//            if textCount < maxCharacterCount {
//                return true
//            }
//            
//            return false
//        }
//        
//        return false
//    }
//    
//}

